﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using BlogApp.Data.Models;
using BlogApp.WP8.Resources;
using BlogApp.WP8.ViewModel;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Practices.ServiceLocation;
using GestureEventArgs = System.Windows.Input.GestureEventArgs;

namespace BlogApp.WP8.View
{
    public partial class MainPage : PhoneApplicationPage
    {
        private MainViewModel _vm;
        private CommentViewModel _commentVm;

        public MainPage()
        {
            InitializeComponent();
       }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            _vm = (MainViewModel) DataContext;
            ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).Text = AppResources.AppBarButtonTextSearch;
            ((ApplicationBarMenuItem) ApplicationBar.MenuItems[0]).Text = AppResources.AppBarButtonTextImprint;

            //_vm.PropertyChanged += OnPropertyChanged;

            base.OnNavigatedTo(e);
        }

        //private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        //{
        //    if (e.PropertyName == "IsOffline")
        //    {
        //        if (!_vm.IsOffline)
        //        {

        //        }
        //    }
        //}


        private void OnCategoryItem_Tap(object sender, GestureEventArgs e)
        {
            var category = (Category)((Grid)sender).DataContext;
            //_vm.OpenBlogPostCommand.Execute(category);
            ServiceLocator.Current.GetInstance<CategoryViewModel>().LoadCategoryCommand.Execute(category);

            Debug.WriteLine("MainPage.xaml.cs | OnCategoryItem_Tap | CategoryId: {0}", category.Id);
        }


        private void OnArticleTapped(object sender, GestureEventArgs e)
        {
            var post = (Post)((StackPanel)sender).DataContext;
            ServiceLocator.Current.GetInstance<PostViewModel>().OpenBlogPostCommand.Execute(post);

            Debug.WriteLine("MainPage.xaml.cs | OnArticleTapped | PostId: {0}", post.Id);
        }

        private void AppBarIconButtonSearch_Click(object sender, EventArgs e)
        {
            ServiceLocator.Current.GetInstance<SearchViewModel>().Cleanup();
            

            _vm.NavigateCommand.Execute("/View/SearchPage.xaml");
        }

        private void AppBarMenuItemImprint_Click(object sender, EventArgs e)
        {
            _vm.NavigateCommand.Execute("/View/ImprintPage.xaml");
        }

        private void RemoveMenuItem_Click(object sender, RoutedEventArgs e)
        {
            var selectedPost = (sender as MenuItem).DataContext as Post; 
            ServiceLocator.Current.GetInstance<PostViewModel>().RemovePostOfflineCommand.Execute(selectedPost);
        }

        private void ContextMenu_Unload(object sender, RoutedEventArgs e)
        {
            var conmen = (sender as ContextMenu);
            conmen.ClearValue(FrameworkElement.DataContextProperty);
        }

        //private void OnPanoramaItem_Changed(object sender, SelectionChangedEventArgs e)
        //{
        //    var selectedItem = e.AddedItems[0] as PanoramaItem;

        //    if (selectedItem != null)
        //    {
        //        ApplicationBar.Mode = (string)selectedItem.Header == AppResources.HeaderItemNews ? ApplicationBarMode.Default : ApplicationBarMode.Minimized;
        //    }
        //}
    }
}